package com.tanhua.management.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.tanhua.management.service.BackgroundDashboardService;
import com.tanhua.management.service.RegisterUserDistributionService;
import com.tanhua.management.vo.SummaryInfoVo;
import com.tanhua.management.vo.UserDistributionVo;
import com.tanhua.management.vo.YearVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Date;

/**
 * @Author lenny
 * @create 2021/9/16 20:44
 */
@RestController
@RequestMapping("dashboard")
@Slf4j
public class DashboardController {

    @Autowired
    private RegisterUserDistributionService registerUserDistributionService;

    @Autowired
    private BackgroundDashboardService backgroundDashboard;


    /**
     * 注册的用户分布
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return UserDistributionVo 封装类对象
     */
    @GetMapping("distribution")
    public ResponseEntity<UserDistributionVo>
    registerUserDistribution(@RequestParam(value = "sd") Long startTime,
                             @RequestParam(value = "ed") Long endTime) {
        try {
            UserDistributionVo userDistributionVo = registerUserDistributionService.registerUserDistribution(startTime, endTime);
            if (ObjectUtil.isEmpty(userDistributionVo)) {
                userDistributionVo = new UserDistributionVo();
            }
            return ResponseEntity.ok(userDistributionVo);
        } catch (Exception e) {
            log.error("出现了错误:", e);
            e.printStackTrace();
        }
        return ResponseEntity.status(500).build();
    }

    /**
     * 查询概要系统计信息
     *
     * @return summaryInfoVo
     */
    @GetMapping("summary")
    public ResponseEntity<SummaryInfoVo> summaryInfoStatistics() throws ParseException {

        SummaryInfoVo summaryInfoVo = backgroundDashboard.summaryInfoStatistics();

        try {
            if (summaryInfoVo != null) {
                return ResponseEntity.ok(summaryInfoVo);
            }
        } catch (Exception e) {
            log.error("出现了错误:", e);
            e.printStackTrace();
        }

        return ResponseEntity.status(500).build();

    }

    /**
     * 新增,活跃用户,次日留存率
     *
     * @param startTime
     * @param endTime
     * @param type      101新增;102活跃用户;103次日留存率
     * @return
     */
    @GetMapping("users")
    public ResponseEntity<YearVo> usersData(@RequestParam("sd") Long startTime,
                                            @RequestParam("ed") Long endTime,
                                            @RequestParam("type") String type) {
        try {
            YearVo yearVo = backgroundDashboard.usersData(startTime, endTime, type);
            if (yearVo != null) {
                return ResponseEntity.ok(yearVo);
            }
        } catch (Exception e) {
            log.error("出现了错误:", e);
        }
        return ResponseEntity.status(500).build();
    }
}
